package com.njworkorder.HTSingleLogin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 用户验证处理
 *
 * @author ht
 */
@Service("UserDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    @Qualifier("qsUserServiceImpl")
    private IQsUserService qsUserService;

    @Autowired
    private SysPermissionService permissionService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
            log.info("进入密码登录");
            QsUser qsUser = qsUserService.selectQsUserByUid(username);
            if (StringUtils.isNull(qsUser))
            {
                log.info("登录用户：{} 不存在.", username);
                throw new ServiceException(MessageUtils.message("user.not.exists"));
            }
            qsUser.setRoleList(new ArrayList<>());
            qsUser.setShowTitle(true);
            log.info("UserDetailsServiceImpl=>qsUser{}",qsUser);
            return createLoginUser(qsUser);
    }

    public UserDetails createLoginUser(QsUser user)
    {
        LoginUser loginUser = new LoginUser(user.getUid(), user.getOuid(), user.getOid(), user, permissionService.getMenuPermission(user), Boolean.TRUE);
        loginUser.setLocal(true);
        return loginUser;
    }
}
